<template>
	<view>
		<!--  -->
		<ben></ben>
		<!--  -->
		<view class="rad2 pad20 min30 background_f">
			<view class="row just_cont_spacebet align_item_c padbottom20" style="border-bottom: 1px solid #f6f6f6;">
				<view class="">
					头像
				</view>
				<view class="row align_item_c" @click="onImage">
					<u-avatar :src="userinfo.avatar" size="50"></u-avatar>
					<view class="minleft20">
						<text class="text-gray">点击上传</text>
					</view>
				</view>
			</view>
			<view class="row just_cont_spacebet align_item_c mintop30 minbottom30 padbottom20"
				style="border-bottom: 1px solid #f6f6f6;">
				<view class="">
					昵称
				</view>
				<input type="text" class="flex1 h-36" style="text-align: right;" v-model="userinfo.nickname"
					placeholder="请输入昵称">
			</view>
			<view class="row just_cont_spacebet align_item_c mintop30 minbottom10">
				<view class="">
					电话号码
				</view>
				<view class="">
					{{ userinfo.phone }}
				</view>
			</view>
		</view>
		<view class="min30 ">
			<u-button text="修改" shape="circle" type="primary" @click="upUserInfo"></u-button>
		</view>
		<view class="min30" v-if="type != 2">
			<u-button text="退出登录" shape="circle" type="primary" color="#FFAD4C" @click="getLogout"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: 0,
				user_id: '',
				userinfo: {}
			};
		},
		onLoad() {
			this.type = uni.getStorageSync('user_type');
			this.user_id = uni.getStorageSync('user_id');
			if (this.type == 2) {
				uni.setNavigationBarTitle({
					title: '修改资料'
				})
			}

		},
		onShow() {
			//当前页面展示的时候他就执行该生命周期
			const token = uni.getStorageSync('token');
			if (token) {
				//如果存在token
				this.getUserinfo()
				//按正常逻辑来讲 登录成功 我们是不是需要向服务器获取用户的信息？
				//那么我们一起来获取用户信息
			} else {
				this.$com.toast('您还未登录，正在前往登录')
				setTimeout(function() {
					uni.navigateTo({
						url: "/mypackage/user/login"
					})
				}, 1000)
			}
		},
		methods: {
			async getUserinfo() {
				let {
					code,
					message,
					data
				} = await this.$com.fetchGet("member/user_info/" + this.user_id)
				if (code == 200) {
					this.userinfo = data

				} else {
					console.log(message);
				}
			},
			async upUserInfo() {
				let {
					code,
					message,
					data
				} = await this.$com.fetchPut("member/user_up/" + this.user_id, {
					nickname: this.userinfo.nickname,
					avatar: this.userinfo.avatar
				})
				if (code == 200) {
					uni.showToast({
						title: message,
						icon: 'success'
					});
					setTimeout(function() {
						uni.reLaunch({
							url: '/pages/user/my'
						});
					}, 1000);

				} else {
					console.log(message);
				}
			},
			onImage() {
				var _this = this
				uni.chooseImage({
					count: 1, // 图片数量
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择或者拍照
					success: (res) => {
						const tempFilePaths = res.tempFilePaths;
						console.log(res)
						uni.showLoading({
							title: '上传中',
							mask: true
						})
						uni.uploadFile({
							url: 'https://xqs197810.com/api/member/upload', //上传图片api
							filePath: tempFilePaths[0],
							name: 'file',
							formData: {
								'user': 'test',
								'ba-user-token': uni.getStorageSync('token')
							},
							header: {
								// token: uni.getStorageSync('token')
								// 'content-type': 'multipart/form-data;boundary=nitamayoubing',
							},
							success: (res) => {
								uni.hideLoading()
								let group = JSON.parse(res.data)
								// console.log(group);
								_this.userinfo.avatar = group.data
								uni.showToast({
									title: "上传成功",
									icon: "success"
								})
							},
							fail(res) {
								console.log(res);
							}
						});
					}
				});
			},
			async getLogout() {
				let {
					code,
					message,
					data
				} = await this.$com.fetchDel("login/user_logout/" + this.user_id)
				if (code == 200) {
					uni.removeStorageSync('user_type');
					uni.removeStorageSync('user_id');
					uni.removeStorageSync('token');
					uni.showToast({
						title: message,
						icon: 'none'
					});
					setTimeout(function() {
						uni.reLaunch({
							url: '/pages/user/index'
						});
					}, 1000);

				} else {
					console.log(message);
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>